<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { systemApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    platform:[] as Array<any>,
    upgradeStatus:[] as Array<any>,
    switchType:[] as Array<any>
  })
  interface formQuert {
    id:number
    version_id:string
    platform_id:number
    upgrade_type:number
    channel_id:number
    download_url:string
    describe:string
    audit_switch:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    version_id: [{ required: true, message: "请输入版本号", trigger: "blur" }],
    platform_id: [{ required: true, message: "请选择平台", trigger: "blur" }],
    upgrade_type: [{ required: true, message: "请选择升级类型", trigger: "blur" }],
    channel_id: [{ required: true, message: "请输入渠道ID", trigger: "blur" }],
    download_url: [{ required: true, message: "请输入下载链接", trigger: "blur" }],
    describe: [{ required: true, message: "请输入版本说明", trigger: "blur" }],
    audit_switch: [{ required: true, message: "请选择升级状态", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { id } = form
      if(id) {
        await systemApi.updateVersions(id,form).then(()=>{
          submitSet = true
          ToastSuccess('编辑成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await systemApi.addVersions(form).then(()=>{
          submitSet = true
          ToastSuccess('新增成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any={})=> {
    const {
      id=0, version_id='', platform_id='', upgrade_type='', audit_switch='',
      channel_id='', download_url='', describe='',
      platform=[], upgradeStatus=[], switchType=[]
    } = query
    action.platform = platform
    action.upgradeStatus = upgradeStatus
    action.switchType = switchType
    if(id) {
      action.title = `编辑版本号：${version_id} 渠道号：${channel_id}`
      Object.assign(form,{
        id, version_id, platform_id, upgrade_type, audit_switch,
        channel_id, download_url, describe
      })
    }else {
      action.title = '新增版本'
    }
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="52%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="版本号" prop="version_id" >
          <el-input v-model="form.version_id" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="平台" prop="platform_id">
          <el-select v-model="form.platform_id" placeholder="请选择平台">
            <el-option
              v-for="item in action.platform"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="升级类型" prop="upgrade_type" >
          <el-select v-model="form.upgrade_type" placeholder="请选择升级类型">
            <el-option
              v-for="item in action.upgradeStatus"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="升级状态" prop="audit_switch" >
          <el-select v-model="form.audit_switch" placeholder="请选择升级状态">
            <el-option
              v-for="item in action.switchType"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="渠道ID" prop="channel_id" >
          <el-input v-model="form.channel_id" placeholder="请输入渠道ID" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="下载链接" prop="download_url" >
          <el-input
            v-model="form.download_url"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入下载链接"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="版本说明" prop="describe" >
          <el-input
            v-model="form.describe"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入版本说明"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>